package com.wfw.feichat.service.impl;

import com.wfw.feichat.entity.User;
import com.wfw.feichat.mapper.UserMapper;
import com.wfw.feichat.service.FriendRequestService;
import com.wfw.feichat.service.FriendService;
import com.wfw.feichat.service.UserService;
import com.wfw.feichat.utils.FileUtils;
import com.wfw.feichat.utils.QRCodeUtils;
import com.wfw.feichat.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

/**
 * @author: wfw
 * @date: 2019-05-21 16:31
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private QRCodeUtils qrCodeUtils;

    @Autowired
    private FriendRequestService friendRequestService;

    @Autowired
    private FriendService friendService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isUsernameExist(String username) {
        User user = new User();
        user.setUsername(username);

        User user1 = userMapper.selectOne(user);
        return user1 != null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User getUserByLogin(String username, String password) {
        Example userExample = new Example(User.class);
        Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", password);
        User user = userMapper.selectOneByExample(userExample);
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public User saveUser(User user) {
        String userId = Sid.nextShort();
        // 判断生成的id是否已存在
        User user1 = userMapper.selectByPrimaryKey(userId);
        while (user1 != null) {
            userId = Sid.nextShort();
            user1 = userMapper.selectByPrimaryKey(userId);
        }

        user.setId(userId);
        user.setImage("");
        user.setImageBig("");
        user.setNickname(user.getUsername());

        // 为每个用户生成一个唯一的二维码
        // feichat_qrcode:[username]
        String content = "feichat_qrcode:" + user.getUsername();
        MultipartFile qrCodeFile = qrCodeUtils.createQRCodeMultipartFile(content);
        String qrCodeFileurl = FileUtils.getFilePath(qrCodeFile);
        user.setQrcode(qrCodeFileurl);

        userMapper.insert(user);

        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public User updateUserInfo(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        return userMapper.selectByPrimaryKey(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User getUserById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User getUserByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        User user = new User();
        user.setUsername(username);
        user = userMapper.selectOne(user);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserVo> getUserVoListByUidList(List<String> uidList) {
        if (uidList == null || uidList.size() <= 0) {
            return null;
        }
        List<UserVo> userVoList = userMapper.getUserListByUidList(uidList);

        return userVoList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void passFriendRequest(String uid, String friendUid) {
        if (StringUtils.isNotBlank(uid) && StringUtils.isNotBlank(friendUid)) {
            friendService.addFriend(uid, friendUid);
            friendService.addFriend(friendUid, uid);
            friendRequestService.removeFriendRequest(friendUid, uid);
            friendRequestService.removeFriendRequest(uid, friendUid);
        }
    }
}
